#include "StdAfx.h"
#include "PictureViewer.h"

namespace StereoCorrespond {

	System::Void PictureViewer::zoomUpdated(float old)
	{
		if(image)
		{
			// set panel size
			DrawPanel->Width = (int)(image->Width*zoomFactor);
			DrawPanel->Height = (int)(image->Height*zoomFactor);

			// set scroll
			clientSizeUpdated();

			// set label
			zoomLabel->Text = "Zoom: " + ZoomFactor.ToString("0.00");
		}
	}

	System::Void PictureViewer::clientSizeUpdated()
	{
		// set scroll
		//if(scrollPanel->HScroll)
		scrollPanel->HorizontalScroll->Value = (int)(-CenterPixel.X*zoomFactor + DrawPanel->ClientSize.Width/2.0f);
		//if(scrollPanel->VScroll)
		scrollPanel->VerticalScroll->Value = (int)(-CenterPixel.Y*zoomFactor + DrawPanel->ClientSize.Height/2.0f);

		// draw image
		DrawPanel->Invalidate();
	}

	System::Void PictureViewer::imageUpdated()
	{
		if(image->Width < ClientSize.Width)
		{
			CenterPixel.X = image->Size.Width/2;
		}
		else
		{
			CenterPixel.X = ClientSize.Width/2;
		}

		if(image->Height < ClientSize.Height)
		{
			CenterPixel.Y = image->Size.Height/2;
		}
		else
		{
			CenterPixel.Y = ClientSize.Height/2;
		}
		
		Features.Clear();
		Current = -1;

		ZoomFactor = 1.0f;
	}
	
	void PictureViewer::clearfeatures()
	{
		Features.Clear();
		Current = -1;
		fsize = 0;
		DrawPanel->Invalidate();
	}

	void PictureViewer::addfeature(int x, int y, float r)
	{
		Features.Insert(fsize++, KeyValuePair<Point, float>(Point(x,y), r));
		DrawPanel->Invalidate();
	}

	bool PictureViewer::next()
	{
		if(Current == -1)
			Current = 0;
		else
			++Current;
		DrawPanel->Invalidate();
		if(Current >= fsize)
		{
			Current = -1;
			return false;
		}
		else
			return true;
	}

	bool PictureViewer::prev()
	{
		if(Current == -1)
			Current = fsize-1;
		else
			--Current;
		DrawPanel->Invalidate();
		if(Current <= -1)
		{
			Current = -1;
			return false;
		}
		else
			return true;
	}

	System::Void PictureViewer::saveToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e)
	{
		if(DialogResult::OK == saveFileDialog->ShowDialog())
		{
			image->Save(saveFileDialog->FileName);
		}
	};
	
	System::Void PictureViewer::DrawPanel_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {
		if(image)
		{
			e->Graphics->DrawImage(image, Rectangle(0, 0, DrawPanel->Width, DrawPanel->Height));  
		}
		else
		{
			e->Graphics->Clear(Color::White);
		}
		
		array<KeyValuePair<Point, float> >^ farray = Features.ToArray();
		for(int i=0; i<fsize; ++i)
		{
			float cx = farray[i].Key.X*zoomFactor;
			float cy = farray[i].Key.Y*zoomFactor;
			float r = (farray[i].Value) * 5.0f * zoomFactor;
				
			e->Graphics->DrawEllipse(Pens::Yellow, cx - r, cy -r, 2*r, 2*r);
			if(i == Current)
				e->Graphics->DrawEllipse(Pens::Red, cx - 10, cy - 10, 20.0, 20.0);
		}
	}

	System::Void PictureViewer::DrawPanel_Resize(System::Object^  sender, System::EventArgs^  e) {
		clientSizeUpdated();
	}

	System::Void PictureViewer::zoomInButton_Click(System::Object^  sender, System::EventArgs^  e) {
		ZoomFactor = ZoomFactor*1.5f;
	}

	System::Void PictureViewer::zoomOutButton_Click(System::Object^  sender, System::EventArgs^  e) {
		ZoomFactor = ZoomFactor/1.5f;
	}

	System::Void PictureViewer::scrollPanel_Scroll(System::Object^  sender, System::Windows::Forms::ScrollEventArgs^  e) {
		// set center
		CenterPixel.X = (int) ((DrawPanel->ClientSize.Width/2.0f - scrollPanel->HorizontalScroll->Value) / zoomFactor);
		CenterPixel.Y = (int) ((DrawPanel->ClientSize.Height/2.0f - scrollPanel->VerticalScroll->Value) / zoomFactor);
	}
}